package com.jiang.firefly;

import java.util.Arrays;

public class ParticleState {
    private double[] dims1;//几维坐标
    private int dimension;
    private double[][] dims2;//几维坐标

    public double f;//适应度，即求解函数值

    public ParticleState(int dim) {
        //这里的位置初始化是有限制的
        this.dimension = dim;
        dims1 = new double[dim];
        dims2 = new double[dim][dim];
        for(int i = 0;i < dim;i++) {
            dims1[i] = Math.random() < 0.5 ? 0:1;//这个初始显然就是0/1
        }
        for(int i = 0;i < dim;i++) {
            for(int j = 0;j < dim;j++) {
                dims2[i][j] = Math.random() < 0.5 ? 0:1;
            }
        }

        for(int i = 0;i < dim;i++) {
            for(int j = 0;j < dim;j++) {
                while(dims1[i] - dims2[i][j] < 0 || sum(dims2, j) < 1){
                    dims2[i][j] = Math.random() < 0.5 ? 0:1;
                    dims1[i] = Math.random() < 0.5 ? 0:1;
                    reInitilize(dims2, j);
                }
            }
        }

    }


    public ParticleState(int dim, double vmax) {
        this.dimension= dim;
        dims1 = new double[dim];
        dims2 = new double[dim][dim];
        for(int i = 0;i< dim;i++) {
            dims1[i] = Math.random()*vmax;
        }
        for(int i = 0;i < dim;i++) {
            for(int j = 0;j < dim;j++) {
                dims2[i][j] = Math.random()*vmax;;
            }
        }
    }

    public double[][] getDims2() {
        return dims2;
    }

    public void setDims2(double[][] dims2) {
        this.dims2 = dims2;
    }

    public int getDimension() {
        return dimension;
    }

    public void setDimension(int dimension) {
        this.dimension = dimension;
    }

    public double[] getDims1() {
        return dims1;
    }

    public void setDims1(double[] dims) {
        this.dims1 = dims1.clone();
    }

    public double sum(double[][] array, int j){
        int sum = 0;
        for(int i = 0;i < dimension;i++) {
            sum += dims2[i][j];
        }
        return sum;
    }

    public void reInitilize(double[][] array, int j){
        for(int i = 0;i < dimension;i++) {
            dims2[i][j] = Math.random() < 0.5 ? 0:1;
        }
    }


    public void add(double[] cal1Num, double[][] cal2Num) {
        for(int i = 0;i<dimension;i++) {
            dims1[i]+=cal1Num[i];
            dims1[i] %= 1;

            dims1[i] = dims1[i] > 0.5 ? 1 : 0;
            for(int j = 0;j < dimension;j++){
                dims2[i][j] += cal2Num[i][j];
                dims2[i][j] %= 1;
                dims2[i][j] = dims2[i][j] > 0.5 ? 1 : 0;
            }
        }
        //移动之后，还要再验证位置是否合法
        for(int i = 0;i < dimension;i++) {
            for(int j = 0;j < dimension;j++) {
                while(dims1[i] - dims2[i][j] < 0 || sum(dims2, j) < 1){
                    dims2[i][j] = Math.random() < 0.5 ? 0:1;
                    dims1[i] = Math.random() < 0.5 ? 0:1;
                    reInitilize(dims2, j);
                }
            }
        }
    }

    public ParticleState clone(){
        ParticleState ps = new ParticleState(dimension);
        ps.setDims1(this.dims1.clone());
        ps.setDims2(this.dims2.clone());
        ps.f = this.f;
        return ps;
    }

    @Override
    public String toString() {
        return "ParticleState{" +
                "dims1=" + Arrays.toString(dims1) +
                ", dimension=" + dimension +
                ", dims2=" + Arrays.toString(dims2) +
                ", f=" + f +
                '}';
    }
}
